package cn.core.tool.aspect;

import cn.core.tool.context.CoreContextData;
import cn.core.tool.eo.DataManipulationLog;
import cn.core.tool.mapper.CoreMapper;
import cn.core.tool.util.CoreTool;
import cn.core.tool.util.Paging;
import com.google.gson.Gson;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.LinkedHashSet;

/**
 * Mapper AOP 拦截器 >>> 批量删除
 */
@Log4j2
@Aspect
@Component
@ConditionalOnProperty(prefix = "proconf", name = "mapper-aspect-remove-batch", havingValue = "true")
public class MapperAspectRemoveBatch {

  @Resource
  private CoreMapper coreMapper;

  @Resource
  private Gson gson;

  @Pointcut("execution(public * cn.core..mapper..*.removeBatch(..))")
  public void removeBatch() {
  }

  @SuppressWarnings("Duplicates")
  @AfterReturning("removeBatch()")
  public void doAfterReturning(JoinPoint joinPoint) throws Exception {
    try {
      for (Object o : joinPoint.getArgs()) {
        if (o != null) {
          if (o instanceof Collection) {
            LinkedHashSet<DataManipulationLog> saveDataManipulationLogSet = new LinkedHashSet<>();
            for (Object eo : (Collection<?>) o) {
              if (eo != null) {
                DataManipulationLog saveDataManipulationLog = new DataManipulationLog()
                    .setId(CoreTool.getUUID32())
                    .setOperationMethod("removeBatch")
                    .setNexusType(CoreContextData.getCurrentContextData() != null ? CoreContextData.getCurrentContextData().getNexusType() : null)
                    .setOperationId(CoreContextData.getCurrentContextData() != null ? CoreContextData.getCurrentContextData().getUsrId() : null)
                    .setOperationAid(CoreContextData.getCurrentContextData() != null ? CoreContextData.getCurrentContextData().getAccountId() : null)
                    .setOperationName(CoreContextData.getCurrentContextData() != null ? CoreContextData.getCurrentContextData().getUsrname() : null)
                    .setOperationTimes(System.currentTimeMillis())
                    .setDataObjectClassName(eo.getClass().getName())
                    .setDataContent(gson.toJson(eo));
                try {
                  saveDataManipulationLog.setDataId((String) eo.getClass().getMethod("getId").invoke(eo));
                } catch (NoSuchMethodException e) {
                }
                saveDataManipulationLogSet.add(saveDataManipulationLog);
              }
            }
            if (CoreTool.isNotEmpty(saveDataManipulationLogSet)) {
              coreMapper.saveBatchDataManipulationLog(new Paging<>(), saveDataManipulationLogSet);
            }
          }
        }
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
  }

}
